package com.qf.controller;

import com.qf.entity.User;
import com.qf.service.PayService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/7/25 17:08
 */
@RestController
@RequestMapping("/payController")
public class PayController {
    @Autowired
    private PayService payService;
    @RequestMapping(value = "/payInfo", method = RequestMethod.POST)
    public ResultEntity payInfo(@RequestBody User user) {
        if (user != null) {
            Float balance = user.getBalance();
            String token = user.getToken();
            Map<String, Object> stringObjectMap = JavaWebToken.parserJavaWebToken(token);
            Integer userId = (Integer) stringObjectMap.get("userId");
            if (userId != null) {
                boolean b = payService.payInfo(userId, balance);
                if (b) {
                    return ResponseUtils.responseClinet(true);
                } else {
                    return ResponseUtils.responseClinet(false);
                }
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }
}
